package com.luhuiguo.cms.server.web;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.luhuiguo.cms.server.cache.Caches;
import com.luhuiguo.cms.server.entity.Channel;
import com.luhuiguo.cms.server.entity.User;
import com.luhuiguo.cms.server.service.ChannelService;
import com.luhuiguo.cms.server.service.UserService;
import com.luhuiguo.cms.server.web.filter.CachingFilter;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.model.Role;

@Controller
public class CmsController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChannelService channelService;

    @RequestMapping("/cms")
    public void cmsHandler() {

    }

    @RequestMapping("/check")
    public void checkHandler(Writer writer) throws IOException {
        writer.write("OK");
    }

    @RequestMapping("/recache")
    public void recacheHandler(Writer writer) throws IOException {
        Caches.getCache(CachingFilter.CACHE_NAME).clear();
        writer.write("OK");
    }

    @RequestMapping("/login")
    public ModelAndView loginHandler(
            @RequestParam(value = "failed", required = false, defaultValue = "false") boolean failed) {
        ModelAndView mav = new ModelAndView("login");
        mav.addObject("failed", failed);
        return mav;
    }

    @RequestMapping("/init")
    public void initHandler(Writer writer) throws IOException {
        if (userService.findUsers().size() < 1) {
            User userAdmin = new User("admin", "manager", "Administrator", Role.ROLE_ADMIN);
            userService.saveUser(userAdmin);
        }
        writer.write(userService.findUsers().toString());

        List<Channel> channels = channelService.findChannels();

        if (channels.size() < 1) {

            Channel site = new Channel(null, "/", "", "首页");
            site.setType(ChannelModel.TYPE_SITE);
            channelService.saveChannel(site);

            Channel bulletin = new Channel(site.getId(), "/bulletin/", "bulletin", "公告");
            channelService.saveChannel(bulletin);

            Channel news = new Channel(site.getId(), "/news/", "news", "新闻资讯");
            channelService.saveChannel(news);
            Channel focus = new Channel(news.getId(), "/news/focus/", "focus", "焦点新闻");
            channelService.saveChannel(focus);
            Channel industry = new Channel(news.getId(), "/news/industry/", "industry", "行业资讯");
            channelService.saveChannel(industry);
            Channel center = new Channel(news.getId(), "/news/center/", "center", "中心动态");
            channelService.saveChannel(center);

            Channel hall = new Channel(site.getId(), "/hall/", "hall", "办事大厅");
            channelService.saveChannel(hall);
            Channel normal = new Channel(hall.getId(), "/hall/normal/", "normal", "普通作品登记");
            channelService.saveChannel(normal);
            Channel art = new Channel(normal.getId(), "/hall/normal/art/", "art", "美术作品");
            channelService.saveChannel(art);
            Channel music = new Channel(normal.getId(), "/hall/normal/music/", "music", "音乐作品");
            channelService.saveChannel(music);
            Channel written = new Channel(normal.getId(), "/hall/normal/written/", "written", "文字作品");
            channelService.saveChannel(written);
            Channel video = new Channel(normal.getId(), "/hall/normal/video/", "video", "影视作品");
            channelService.saveChannel(video);
            Channel graphic = new Channel(normal.getId(), "/hall/normal/graphic/", "graphic", "图形作品");
            channelService.saveChannel(graphic);
            Channel other = new Channel(normal.getId(), "/hall/normal/other/", "other", "其他作品类型");
            channelService.saveChannel(other);
            Channel textile = new Channel(normal.getId(), "/hall/normal/textile/", "textile", "家纺作品");
            channelService.saveChannel(textile);
            Channel anime = new Channel(normal.getId(), "/hall/normal/anime/", "anime", "动漫作品");
            channelService.saveChannel(anime);
            Channel printing = new Channel(normal.getId(), "/hall/normal/printing/", "printing", "印刷包装作品");
            channelService.saveChannel(printing);
            Channel transfer = new Channel(normal.getId(), "/hall/normal/transfer/", "transfer", "著作权转让登记");
            channelService.saveChannel(transfer);
            Channel contract = new Channel(normal.getId(), "/hall/normal/contract/", "contract", "著作权合同登记");
            channelService.saveChannel(contract);

            Channel software = new Channel(hall.getId(), "/hall/software/", "software", "软件著作权登记");
            channelService.saveChannel(software);
            Channel registration = new Channel(software.getId(), "/hall/software/registration/", "registration",
                    "计算机软件著作权登记");
            channelService.saveChannel(registration);
            Channel transferee = new Channel(software.getId(), "/hall/software/transferee/", "transferee",
                    "计算机软件著作权受让（转让）登记");
            channelService.saveChannel(transferee);
            Channel change = new Channel(software.getId(), "/hall/software/change/", "change", "软件登记变更事项或补充登记");
            channelService.saveChannel(change);
            Channel softcontractreg = new Channel(software.getId(), "/hall/software/softcontractreg/",
                    "softcontractreg", "软件著作权转让合同或专有合同登记");
            channelService.saveChannel(softcontractreg);
            Channel reissue = new Channel(software.getId(), "/hall/software/reissue/", "reissue", "补发或换发软件著作权登记证书申请");
            channelService.saveChannel(reissue);
            Channel pledge = new Channel(software.getId(), "/hall/software/pledge/", "pledge", "软件著作权质押合同登记");
            channelService.saveChannel(pledge);

            Channel tech = new Channel(hall.getId(), "/hall/tech/", "tech", "双软认定/高新认定");
            channelService.saveChannel(tech);
            Channel product = new Channel(tech.getId(), "/hall/tech/product/", "product", "软件产品登记");
            channelService.saveChannel(product);
            Channel testing = new Channel(tech.getId(), "/hall/tech/testing/", "testing", "软件评测");
            channelService.saveChannel(testing);
            Channel identify = new Channel(tech.getId(), "/hall/tech/identify/", "identify", "软件企业认定");
            channelService.saveChannel(identify);
            Channel examined = new Channel(tech.getId(), "/hall/tech/examined/", "examined", "软件企业年审");
            channelService.saveChannel(examined);
            Channel hangzhou = new Channel(tech.getId(), "/hall/tech/hangzhou/", "hangzhou", "杭州市高新技术企业认定");
            channelService.saveChannel(hangzhou);
            Channel national = new Channel(tech.getId(), "/hall/tech/national/", "national", "国家级高新技术企业认定");
            channelService.saveChannel(national);

            Channel business = new Channel(site.getId(), "/business/", "business", "版权贸易");
            channelService.saveChannel(business);
            Channel policy = new Channel(business.getId(), "/business/policy/", "policy", "政策中心");
            channelService.saveChannel(policy);
            Channel transaction = new Channel(business.getId(), "/business/transaction/", "transaction", "交易服务");
            channelService.saveChannel(transaction);
            Channel financial = new Channel(business.getId(), "/business/financial/", "financial", "融资服务");
            channelService.saveChannel(financial);
            Channel agent = new Channel(business.getId(), "/business/agent/", "agent", "版权经纪人");
            channelService.saveChannel(agent);
            Channel cooperation = new Channel(business.getId(), "/business/cooperation/", "cooperation", "合作专区");
            channelService.saveChannel(cooperation);
            Channel creativity = new Channel(business.getId(), "/business/creativity/", "creativity", "创意集市");
            channelService.saveChannel(creativity);

            Channel services = new Channel(site.getId(), "/services/", "services", "知识产权法律服务");
            channelService.saveChannel(services);
            Channel fabric = new Channel(services.getId(), "/services/fabric/", "fabric", "中国布艺联盟浙江中心");
            channelService.saveChannel(fabric);
            Channel rights = new Channel(services.getId(), "/services/rights/", "rights", "维权代理");
            channelService.saveChannel(rights);
            Channel timestamp = new Channel(services.getId(), "/services/timestamp/", "timestamp", "文件出生证在线申报");
            channelService.saveChannel(timestamp);
            Channel identification = new Channel(services.getId(), "/services/identification/", "identification",
                    "知识产权鉴定");
            channelService.saveChannel(identification);
            Channel stories = new Channel(services.getId(), "/services/stories/", "stories", "成功案例");
            channelService.saveChannel(stories);
            Channel laws = new Channel(services.getId(), "/services/laws/", "laws", "法律法规");
            channelService.saveChannel(laws);

            Channel mcsc = new Channel(site.getId(), "/mcsc/", "mcsc", "音乐许可");
            channelService.saveChannel(mcsc);
            Channel mcscintro = new Channel(mcsc.getId(), "/mcsc/mcscintro/", "mcscintro", "音著协介绍");
            channelService.saveChannel(mcscintro);
            Channel fees = new Channel(mcsc.getId(), "/mcsc/fees/", "fees", "收费标准");
            channelService.saveChannel(fees);
            Channel legal = new Channel(mcsc.getId(), "/mcsc/legal/", "legal", "法律依据");
            channelService.saveChannel(legal);
            Channel copy = new Channel(mcsc.getId(), "/mcsc/copy/", "copy", "复制标准");
            channelService.saveChannel(copy);
            Channel performance = new Channel(mcsc.getId(), "/mcsc/performance/", "performance", "表演标准");
            channelService.saveChannel(performance);
            Channel downloads = new Channel(mcsc.getId(), "/mcsc/downloads/", "downloads", "表格下载");
            channelService.saveChannel(downloads);

            Channel faq = new Channel(site.getId(), "/faq/", "faq", "常见问题");
            channelService.saveChannel(faq);

            Channel guide = new Channel(site.getId(), "/guide/", "guide", "操作指南");
            channelService.saveChannel(guide);

            Channel download = new Channel(site.getId(), "/download/", "download", "表格下载");
            channelService.saveChannel(download);

            Channel about = new Channel(site.getId(), "/about/", "about", "关于我们");
            channelService.saveChannel(about);
            Channel intro = new Channel(about.getId(), "/about/intro/", "intro", "中心介绍");
            channelService.saveChannel(intro);
            Channel organization = new Channel(about.getId(), "/about/organization/", "organization", "组织架构");
            channelService.saveChannel(organization);
            Channel functions = new Channel(about.getId(), "/about/functions/", "functions", "职能介绍");
            channelService.saveChannel(functions);

            writer.write(channelService.findChannels().toString());

        } else {
            for (Channel c : channels) {
                String path = c.getPath();
                if (!StringUtils.startsWith(path, "/")) {
                    path = "/" + path;

                }
                if (!StringUtils.endsWith(path, "/")) {
                    path = path + "/";
                }

                if (!StringUtils.equals(path, c.getPath())) {
                    c.setPath(path);
                    channelService.updateChannel2(c);
                    System.out.println(c);

                }

            }
            writer.write(channelService.findChannels().toString());

        }

    }

}
